package com.zhy.skinchangenow.image

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.ImageView
import android.widget.Toast
import com.water.skin.R
import com.water.skin.SkinManager
import com.water.skin.callback.ISkinChangingCallback
import com.zhy.skinchangenow.databinding.ActivityDynamicImageBinding
import java.io.File
import java.lang.Exception

/**
 *  Created by zxn on 2021/9/2.
 */
class DynamicImageActivity : AppCompatActivity() {

    companion object {
        @JvmStatic
        fun jumpTo(context: Context) {
            context.startActivity(Intent(context, DynamicImageActivity::class.java))
        }

//        private val mSkinPkgPath =
//            Environment.getExternalStorageDirectory().toString() + File.separator + "skin_plugin.apk"
        private val mSkinPkgPath =
            Environment.getExternalStorageDirectory().toString() + File.separator + "plugin.apk"
    }

    private var mIvChange: ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SkinManager.getInstance().register(this)
        ActivityDynamicImageBinding.inflate(layoutInflater).run {
            setContentView(root)
            mIvChange = ivChange

            btnChange.setOnClickListener {
                changeImage(ivChange)
            }
        }
        // android:tag="skin:ic_fish:src"
    }

    private var type = 0

    private fun changeImage(ivChange: ImageView) {
        //----->/storage/emulated/0/skin_plugin.apk

        type++
        ivChange.setTag(R.id.skin_tag_id,if (type % 2 == 0) "skin:ic_fish:src" else "skin:ic_bottle_ball_camo:src")

        SkinManager.getInstance()
            .changeSkin(mSkinPkgPath, "com.zhy.plugin", object : ISkinChangingCallback {
                override fun onStart() {}
                override fun onError(e: Exception) {
                    Toast.makeText(this@DynamicImageActivity, "换肤失败:" + e.message, Toast.LENGTH_SHORT)
                        .show()
                }

                override fun onComplete() {
                    Toast.makeText(this@DynamicImageActivity, "换肤成功", Toast.LENGTH_SHORT).show()
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        SkinManager.getInstance().unregister(this)
    }

}